<?php

namespace App\Utils;

class ApiResponse
{
    private static function toJson(int $code, string $msg = "成功", array $data)
    {
        echo json_encode([
            'code' => $code,
            'msg'  => $msg,
            'datas' => $data,
        ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        die;
    }

    public static function success($data = [], $code = 0, $msg = "成功")
    {
        self::toJson($code, $msg, $data);
    }

    public static function fail($msg = "失败", $code = -1)
    {
        self::toJson($code, $msg, []);
    }
}
